<?php
/*
 * Last Edited: 27-7-2013
 */
class UserModel extends Zend_Db_Table_Abstract{
	protected $_name = 'user';
	protected $_primary = 'user_id';
    protected $_itemPerPage = 5;
    protected $_id = 'user_id';
    public function getUserInfo($params){
    	$db = Zend_Registry::get("connectDB");
    	$mysql = $db->select()
    	->from("user")
    	->join('user_game', 'user.user_id = user_game.user_id', array("level_down","level", "experiment", "win", "lose", "total_win", "total_lose","achievement_id", "star"))
    	->where("user.user_id = ?", $params['user_id'])
    	->where("game_id = ?", $params["game_id"]);
    	$result = $db->fetchRow($mysql);
    	if(!$result){
    		Zend_Loader::loadClass("UserGameModel");
   			$m = new UserGameModel();
   			$m->insert(array(
   					"user_id" => $params['user_id'],
   					"game_id" => $params["game_id"]
   			));
   			return $this->getUserInfo($params);
    	}
    	return $result;
    }
    public function updateInfo($data, $user_id, $game_id){
    	$db = Zend_Registry::get("connectDB");
    	$sql =
	    "UPDATE user
	    JOIN user_game ON user.user_id = user_game.user_id AND user_game.game_id = ".$game_id.
	    " SET ";
    	foreach ($data as $key => $d){
    		$sql .= $key." = ".$d . ", ";
    	}
    	$sql .= "user_game.update_time = NOW() ".
	    "WHERE user.user_id = ".$user_id;
    	$db->query($sql);
    }
    public function getUserById($user_id){
    	$db = Zend_Registry::get("connectDB");
    	$mysql = $db->select()
    	->from("user")
    	->where("user_id = ?", $user_id);
    	$result = $db->fetchRow($mysql);
    	return $result;
    }
    public function checkAccount($params){
        $db = Zend_Registry::get("connectDB");
        $mysql = $db->select()
                    ->from("user")
                    ->where("user_name = ?", $params['user_name']);
        if(isset($params['password'])){
            $mysql->where("password = ?", md5($params['password']));
        }
        $result = $db->fetchRow($mysql);        
        return $result;
    }
    public function checkAccountExist($username, $email){
    	$db = Zend_Registry::get("connectDB");
    	$mysql = $db->select()
    	->from("user")
    	->where("user_name = ?", $username);
    	
    	$result = $db->fetchRow($mysql);
    	if($result)
    		return false;
    	$mysql = $db->select()
    	->from("system_user")
    	->where("user_name = ?", $username);
    	$result = $db->fetchRow($mysql);
    	if($result) return false ;
    	return true;
    }
    
    public function checkLogin($params){        
        $auth = new Service_Auth();
        return $auth->login($params);
    }
    public function getListTopUser($limit, $game_id ){
    	$db = Zend_Registry::get("connectDB");
    	$mysql = $db->select()
    	->from("user_game", array("level", "experiment","achievement_id", "star","total_win","total_lose"))
    	->joinLeft("user", "user.user_id=user_game.user_id", array("user_name", "avatar"))
    	->joinLeft("system_user", "system_user.id=user_game.sys_user_id", array("sys_user_name" => "user_name", "sys_avatar" => "avatar"))
    	->where("game_id = ?", $game_id)
    	->order("experiment DESC")
    	->limit($limit);
    	$result = $db->fetchAll($mysql);
    	return $result;
    }
    public function addBlacklist($params){
        if(!empty($params)){
            $db = Zend_Registry::get("connectDB");
            $db->insert("black_list_user", array("email"=>$params['email']));    
        }
    }    
}